﻿using System;
using System.Collections.Generic;
using System.Text;

class PolynomialSum
{
    //Exercise 11.
    //Write a method that adds two polynomials. Represent them as arrays of their coefficients as in the example below:
    //x2 + 5 = 1x2 + 0x + 5 -> {5, 0, 1}

    static void Main()
    {
        int[] firstPolynominal = { 5, 0, 1 };
        int[] secondPolynominal = { -7, 6, 9 };

        AddPolynominals(firstPolynominal, secondPolynominal);
    }

    private static void AddPolynominals(int[] firstPolynominal, int[] secondPolynominal)
    {
        List<int> resultPolynominal = new List<int>();

        if (firstPolynominal.Length >= secondPolynominal.Length)
        {
            for (int i = 0; i < firstPolynominal.Length; i++)
            {
                resultPolynominal.Add(firstPolynominal[i] + secondPolynominal[i]);
            }
        }
        else
        {
            for (int i = 0; i < secondPolynominal.Length; i++)
            {
                resultPolynominal.Add(firstPolynominal[i] + secondPolynominal[i]);
            }
        }

        //print the result polynominal
        StringBuilder resultPolynominalStr = new StringBuilder();

        if (resultPolynominal[0] != 0)
        {
            resultPolynominalStr.AppendFormat("{0}", resultPolynominal[0]);
        }

        for (int i = 1; i < resultPolynominal.Count; i++)
        {
            if (resultPolynominal[i] != 0 && resultPolynominal[i] > 0)
            {
                resultPolynominalStr.AppendFormat(" + {0} * x^{1}", resultPolynominal[i], i);
            }
            if (resultPolynominal[i] < 0)
            {
                resultPolynominalStr.AppendFormat("{0} * x^{1}", resultPolynominal[i], i);
            }
        }
        Console.WriteLine(resultPolynominalStr.ToString());
    }
}

